<template>
  <div class="login-page">
    <!-- 通栏导航 -->
    <van-nav-bar title="面经登录"/>
    <!-- 用户名/密码 -->
    <van-form @submit="onSubmit">
    <van-field
    v-model="username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' ,trigger:'onChange'},
    {pattern:/^\w{5,11}$/,message:'请填写5-11位用户名',trigger:'onChange'}
    ]"
  />
    <van-field
    v-model="password"
    type="password"
    name="password"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' ,trigger:'onChange'},
    {pattern:/^\w{6,15}$/,message:'请填写6-15位密码',trigger:'onChange'}
    ]"
  />
  <div style="margin: 16px;">
    <van-button block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
<router-link class="link" to="/register">注册账号</router-link>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { setToken } from '@/utils/storage'
import { reqLogin } from '@/api/user'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // 1.发送请求
      const { data: { token } } = await reqLogin(values)
      // 2.存储token，调用方法存储
      setToken(token)
      // 3.提示
      Toast.success('登录成功')
      // 4.跳转到面经页面
      this.$router.push('/')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
